//
//  LiveSildingTableView.m
//  project-demo-oc
//
//  Created by 赵隆杰 on 2021/6/15.
//

#import "LiveSildingTableView.h"
#import "LiveSildingModel.h"
#import "LiveSildingTableViewCell.h"

@interface LiveSildingTableView () <UITableViewDataSource, UITableViewDelegate, LiveSildingTableViewCellDelegate>
{
    LiveSildingTableViewCell *_lastCell;// 上一个cell
    NSInteger _totalCount;
}

@property (nonatomic, strong) NSMutableArray<LiveSildingModel *> *playerList;

@end

@implementation LiveSildingTableView

- (instancetype)init {
    self = [super init];
    if (self) {
    
        self.backgroundColor = [UIColor lightGrayColor];
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        self.pagingEnabled = YES;
        self.clipsToBounds = NO;
        self.showsVerticalScrollIndicator = NO;
        self.showsHorizontalScrollIndicator = NO;
        self.scrollsToTop = NO;
        self.estimatedRowHeight = SL_WINDOW_HEIGHT;
        self.estimatedSectionHeaderHeight = 0;
        self.estimatedSectionFooterHeight = 0;
        self.dataSource = self;
        self.delegate = self;
        [self registerNib:[UINib nibWithNibName:NSStringFromClass([LiveSildingTableViewCell class]) bundle:[NSBundle mainBundle]] forCellReuseIdentifier:NSStringFromClass([LiveSildingTableViewCell class])];
    }
    return self;
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.playerList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LiveSildingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LiveSildingTableViewCell class]) forIndexPath:indexPath];
    cell.delegate = self;
    cell.model = self.playerList[indexPath.row];
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return SL_WINDOW_HEIGHT;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat y = scrollView.contentOffset.y;
    if (y - (int)y > 0) { return; }
    if ((int)y % (int)SL_WINDOW_HEIGHT == 0) {
        
        NSInteger index = y/SL_WINDOW_HEIGHT;
        
        LiveSildingTableViewCell *videoCell = [self cellForRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]];
        
        if (videoCell != nil && videoCell.model != nil) {
            
            if (videoCell != _lastCell) {
                if (_lastCell) {
                    
                    [_lastCell stopPlay];
                    
                    if (index > 1) {
                        if (self.playerTableDelegate && [self.playerTableDelegate respondsToSelector:@selector(pre_fetchMoreVideoList:)]) {
                            [self.playerTableDelegate pre_fetchMoreVideoList:self.playerList.count];
                        }
                    }
                    if (index < 1) {
                        if (self.playerTableDelegate && [self.playerTableDelegate respondsToSelector:@selector(pre_fetchMoreFrontVideoList:)]) {
                            [self.playerTableDelegate pre_fetchMoreFrontVideoList:self.playerList.count];
                        }
                    }
                    
                }
                
                _lastCell = videoCell;
                
                if (_totalCount == 1 || _totalCount == 2) {
                    [videoCell startPlay];
                } else if (index == 1) {
                    [videoCell startPlay];
                }
                
            }
            
        }
        
    }
    
}

- (void)reloadList:(NSMutableArray <LiveSildingModel *>*)list
{
    _playerList = list;
    [self reloadData];
    
//    if (@available(iOS 11.0, *)) {
//        [UIView setAnimationsEnabled:NO];
//        [self performBatchUpdates:^{
//            [self reloadData];
//        } completion:^(BOOL finished) {
//            [UIView setAnimationsEnabled:YES];
//        }];
//    } else {
//        // Fallback on earlier versions
//        [self reloadData];
//    }
    
}

- (void)backBtnClick
{
    if (self.playerTableDelegate && [self.playerTableDelegate respondsToSelector:@selector(backBtnClick)]) {
        [self.playerTableDelegate backBtnClick];
    }
}

- (void)first_didScrollView:(NSInteger)count
{
    _totalCount = count;
    [self scrollViewDidScroll:self];
}

@end
